//
//  ScaleSegue.swift
//  PamperedPets
//
//  Created by xiaolei on 15/10/23.
//  Copyright © 2015年 Caroline Begbie. All rights reserved.
//

import UIKit


protocol ViewScaleable{
    var scaleView:UIView {get}
}

class ScaleSegue: UIStoryboardSegue {
   
     override func perform(){
        destinationViewController.transitioningDelegate = self
        super.perform()
    }
}

extension ScaleSegue:UIViewControllerTransitioningDelegate{
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return ScalePresentAnimator()
    }
}



class ScalePresentAnimator : NSObject,
UIViewControllerAnimatedTransitioning {
    //动画持续的时间（一般时间比较短，这里设置的比较长，是为了明显的看到效果）
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval{
        return 2.0
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning){
        
        // 1 获取到目标视图的控制器和View
        let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
//        let toView = toViewController.view
        //获取原视图的视图控制器和View
        var fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
        
        if let fromNC = fromViewController as? UINavigationController{
            if let controller = fromNC.topViewController{
                fromViewController = controller
            }
        }
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        // 2 添加 toView fromView到transiton的context
        if let toView = toView,fromView = fromView{
            transitionContext.containerView()?.insertSubview(toView, belowSubview: fromView)
        }
        
        //3 Set up the initial state for the animation
//        toView?.frame = .zero
        
        var startFrame = CGRect.zero
        if let fromViewController = fromViewController as? ViewScaleable{
            startFrame = fromViewController.scaleView.frame
        }else{
            print("Warning: Controller \(fromViewController) does not"+"conform to ViewScaleable")
        }
        toView?.frame = startFrame
        
        toView?.layoutIfNeeded()
        
        
        //4 preform the animation
        let duration = transitionDuration(transitionContext)
        let finalFrame = transitionContext.finalFrameForViewController(toViewController)
        
        UIView.animateWithDuration(duration, animations: { () -> Void in
            toView?.frame = finalFrame
            toView?.layoutIfNeeded()
            fromView?.alpha = 0.0
            
            }) { (_) -> Void in
                
                fromView?.alpha = 1.0
                transitionContext.completeTransition(true)
                
        }
        
    }
}